package cn.iocoder.yudao.module.ems.service.eqtype;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.ems.controller.admin.eqtype.vo.*;
import cn.iocoder.yudao.module.ems.dal.dataobject.eqtype.EqTypeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.ems.convert.eqtype.EqTypeConvert;
import cn.iocoder.yudao.module.ems.dal.mysql.eqtype.EqTypeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ems.enums.ErrorCodeConstants.*;

/**
 * 设备类别 Service 实现类
 *
 * @author 左达成
 */
@Service
@Validated
public class EqTypeServiceImpl implements EqTypeService {

    @Resource
    private EqTypeMapper eqTypeMapper;

    @Override
    public Long createEqType(EqTypeCreateReqVO createReqVO) {
        // 插入
        EqTypeDO eqType = EqTypeConvert.INSTANCE.convert(createReqVO);
        eqTypeMapper.insert(eqType);
        // 返回
        return eqType.getId();
    }

    @Override
    public void updateEqType(EqTypeUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateEqTypeExists(updateReqVO.getId());
        // 更新
        EqTypeDO updateObj = EqTypeConvert.INSTANCE.convert(updateReqVO);
        eqTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteEqType(Long id) {
        // 校验存在
        this.validateEqTypeExists(id);
        // 删除
        eqTypeMapper.deleteById(id);
    }

    private void validateEqTypeExists(Long id) {
        if (eqTypeMapper.selectById(id) == null) {
            throw exception(EQ_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public EqTypeDO getEqType(Long id) {
        return eqTypeMapper.selectById(id);
    }

    @Override
    public List<EqTypeDO> getEqTypeList(Collection<Long> ids) {
        return eqTypeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<EqTypeDO> getEqTypePage(EqTypePageReqVO pageReqVO) {
        return eqTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<EqTypeDO> getEqTypeList(EqTypeExportReqVO exportReqVO) {
        return eqTypeMapper.selectList(exportReqVO);
    }

    @Override
    public List<EqTypeDO> getSimpleEqTypes(Collection<Long> ids) {
        return eqTypeMapper.selectBatchIds(ids);
    }

    @Override
    public List<EqTypeDO> getSimpleEqTypes(EqTypeListReqVO reqVO) {
        return eqTypeMapper.selectList(reqVO);
    }

}
